﻿#include "CannibalTentacles.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"
#include "../../MirEnvir/Map.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    unsigned char CannibalTentacles::getAttackRange() const
    {
        return 8;
    }

    CannibalTentacles::CannibalTentacles(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool CannibalTentacles::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), getAttackRange());
    }

    void CannibalTentacles::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;
        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        if (!ranged)
        {
            if (getEnvir()->Random->Next(5) > 0)
            {
                S::ObjectAttack *tempVar = new S::ObjectAttack();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = getCurrentLocation();
                Broadcast(tempVar);

                int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
                if (damage == 0)
                {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                    return;
                }

                DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::AC, false});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                S::ObjectAttack *tempVar2 = new S::ObjectAttack();
                tempVar2->ObjectID = ObjectID;
                tempVar2->Direction = getDirection();
                tempVar2->Location = getCurrentLocation();
                tempVar2->Type = 1;
                Broadcast(tempVar2);
                HalfmoonAttack(500);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }
        }
        else
        {
            S::ObjectRangeAttack *tempVar3 = new S::ObjectRangeAttack();
            tempVar3->ObjectID = ObjectID;
            tempVar3->Direction = getDirection();
            tempVar3->Location = getCurrentLocation();
            tempVar3->TargetID = getTarget()->ObjectID;
            tempVar3->Type = 0;
            Broadcast(tempVar3);

            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
                return;
            }

            int delay = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation()) * 50 + 500; //50 MS per Step
            DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + delay, {getTarget(), damage, DefenceType::MACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
        }
    }

    void CannibalTentacles::HalfmoonAttack(int delay, DefenceType defenceType)
    {
        MirDirection dir = Functions::PreviousDir(getDirection());

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }

        for (int i = 0; i < 4; i++)
        {
            Point *target = Functions::PointMove(getCurrentLocation(), dir, 1);
            dir = Functions::NextDir(dir);

            if (!getCurrentMap()->ValidPoint(target))
            {
                continue;
            }

            Cell *cell = getCurrentMap()->GetCell(target);
            if (cell->Objects.empty())
            {
                continue;
            }

            for (int o = 0; o < cell->Objects.size(); o++)
            {
                MapObject *ob = cell->Objects[o];
                if (ob->getRace() != ObjectType::Player && ob->getRace() != ObjectType::Monster)
                {
                    continue;
                }
                if (!ob->IsAttackTarget(this))
                {
                    continue;
                }

                DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + delay, {ob, damage, defenceType, true});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
                break;
            }
        }
    }

    void CannibalTentacles::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);
        bool poison = std::any_cast<bool>(data[3]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        if (poison && target->Attacked(this, damage, defence) > 0)
        {
            PoisonTarget(target, 1, 5, PoisonType::Green, 1000);
        }
    }

    void CannibalTentacles::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }

        if (InAttackRange() && getCanAttack())
        {
            Attack();
            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        MoveTo(getTarget()->getCurrentLocation());
    }
}
